/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.dataaccess.dao.conference;

import com.nus.dataaccess.dao.author.Author;
import com.nus.dataaccess.dao.base.BaseEntity;
import com.nus.dataaccess.dao.review.ReviewAssignment;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author robin.foe
 */

@Entity
@Table(name="CONFERENCE_AUTHOR")
public class ConferenceAuthor extends BaseEntity{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    public Long getId() {return id;}
    public void setId(Long id) {this.id = id;}
    @Override
    public Serializable getPk() {return this.getId();}

    @Column(name="CONFERENCE_ID")
    private Long conferenceId;
    public Long getConferenceId() {return conferenceId;}
    public void setConferenceId(Long conferenceId) {this.conferenceId = conferenceId;}
    
    @Column(name="AVG_SCORE")
    private BigDecimal avgScore;
    public BigDecimal getAvgScore() {return avgScore;}
    public void setAvgScore(BigDecimal avgScore) {this.avgScore = avgScore;}
        
    @Column(name="PUBLISH_STATUS", length=1, columnDefinition="CHAR(1)")
    private String publishStatus;
    public String getPublishStatus() {return publishStatus;}
    public void setPublishStatus(String publishStatus) {this.publishStatus = publishStatus;}
    
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="CONFERENCE_ID", referencedColumnName="ID",insertable=false,updatable=false)
    private Conference conference;
    public Conference getConference() {return conference;}
    public void setConference(Conference conference) {
        this.conference = conference;
        this.setConferenceId((conference!= null ) ? conference.getId() : null);
    }

    @OneToMany(mappedBy="conferenceAuthor",cascade= CascadeType.REFRESH,orphanRemoval=true)
    private List<ConferenceItem> conferenceItems = new ArrayList<ConferenceItem>();
    public List<ConferenceItem> getConferenceItems() {return conferenceItems;}
    public void setConferenceItems(List<ConferenceItem> conferenceItems) {this.conferenceItems = conferenceItems;}
    
    @OneToMany(mappedBy="conferenceAuthor",cascade= CascadeType.REFRESH,orphanRemoval=true)
    private List<Author> authors = new ArrayList<Author>();
    public List<Author> getAuthors() {return authors;}
    public void setAuthors(List<Author> authors) {this.authors = authors;}
    
    @OneToMany(mappedBy="conferenceAuthor",cascade= CascadeType.REFRESH,orphanRemoval=true)
    private List<ReviewAssignment> reviewer = new ArrayList<ReviewAssignment>();
    public List<ReviewAssignment> getReviewer() {return reviewer;}
    public void setReviewer(List<ReviewAssignment> reviewer) {this.reviewer = reviewer;}
    public List<ReviewAssignment> getCompletedReviews(){
        List<ReviewAssignment> reviews = new ArrayList<ReviewAssignment>();
        for(ReviewAssignment reviewAssigment : reviewer){
            if(!reviewAssigment.isCompleted())
                continue;
            
            reviews.add(reviewAssigment);
        }
        return reviews;
    }
    
    public BigDecimal getAverageReview(){
        BigDecimal totalCount = new BigDecimal(0);
        BigDecimal totalDivider = new BigDecimal (0);
        for(ReviewAssignment reviewAssigment : this.getCompletedReviews()){
            totalCount = totalCount.add(new BigDecimal(reviewAssigment.getTotalScore()));
            totalDivider = totalDivider.add(new BigDecimal(50));
        }
        if(totalCount.compareTo(new BigDecimal(0)) == 0)
            return totalCount;
       
        totalCount = totalCount.divide(totalDivider,2,BigDecimal.ROUND_HALF_UP);
        totalCount = totalCount.multiply(new BigDecimal(100));
        return totalCount;
    }
    
    public void populateAverageScore(){
        this.avgScore = this.getAverageReview();
    }

}
